package com.nexus.service.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nexus.core.exception.ServiceException;
import com.nexus.core.service.TreeService;
import com.nexus.core.sys.entity.SysOffice;
import com.nexus.core.sys.dto.SysOfficeDTO;
import com.nexus.core.utils.ObjectUtils;
import com.nexus.service.sys.mapper.SysOfficeMapper;
import com.nexus.service.sys.service.mapstruct.SysOfficeWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 部门Service
 *
 * @author 路正宁
 * @version 2023-03-15
 */
//@DS("zxhy_platform_db")
@Service
@Transactional(rollbackFor = {ServiceException.class,Exception.class, RuntimeException.class})
public class SysOfficeService extends TreeService<SysOfficeMapper, SysOffice, SysOfficeDTO,SysOfficeWrapper> {

    @Autowired
    SysOfficeWrapper sysOfficeWrapperImpl;

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public SysOfficeDTO findById(String id) {
        return baseMapper.findById(id);
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    public List<SysOfficeDTO> listDTO() {
        return baseMapper.findList();
    }

    public boolean saveOrUpdate(SysOffice sysOffice) {
        return super.saveOrUpdate(sysOffice);
    }



    public IPage<SysOfficeDTO> findPage(Page<SysOfficeDTO> page, QueryWrapper queryWrapper) {
        queryWrapper.eq("a.deleted", 0); // 排除已经删除
        return baseMapper.findList(page, queryWrapper);
    }

    /**
     * 批量保存部门
     * @Date 2023/8/23 17:04
     * @Author luzhengning
     **/
    public List<SysOfficeDTO> saveOrUpdateBatch(List<SysOfficeDTO> sysOfficeDTOS) {
        List<SysOffice> entity = sysOfficeWrapperImpl.toEntity(sysOfficeDTOS);
        if(saveOrUpdateBatch(entity)){
            return sysOfficeWrapperImpl.toDTO(entity);
        }
        return null;
    }

    /**
     * 删除机构关联
     * @Date 2023/9/6 10:06
     * @Author luzhengning
     **/
    public boolean deleteByOrgIds(List<String> orgIds){
        if(ObjectUtils.isEmpty(orgIds))return false;
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.in("sys_org",orgIds);
        return this.remove(queryWrapper);
    }

}
